package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BRecyclingCarbon;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BRecyclingCarbonMapper;
import com.isdepci.project.carbon.service.IBRecyclingCarbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 回收利用碳汇Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BRecyclingCarbonServiceImpl implements IBRecyclingCarbonService
{
    @Autowired
    private BRecyclingCarbonMapper bRecyclingCarbonMapper;


    @Override
    public AjaxResult calRecyclingCarbon(Long projectId) {
        // 根据 projectId 查询 BRecyclingCarbon 数据
        List<BRecyclingCarbon> bRecyclingCarbons = bRecyclingCarbonMapper.selectBRecyclingCarbonByProjectId(projectId);

        // 初始化总回收利用碳汇量
        BigDecimal totalRecyclingCarbon = BigDecimal.ZERO;

        // 遍历每条数据并计算回收利用碳汇量
        for (BRecyclingCarbon recyclingCarbon : bRecyclingCarbons) {
            // 计算每条数据的回收利用碳汇量
            BigDecimal recyclingCarbonValue = recyclingCarbon.getCo2Volume()
                    .multiply(recyclingCarbon.getCo2Purity().divide(BigDecimal.valueOf(100)))
                    .multiply(BigDecimal.valueOf(19.7));
            recyclingCarbon.setCarbonEmissions(recyclingCarbonValue);
            bRecyclingCarbonMapper.updateBRecyclingCarbon(recyclingCarbon);
            // 累加回收利用碳汇量
            totalRecyclingCarbon = totalRecyclingCarbon.add(recyclingCarbonValue);
        }
        // 返回计算结果
        return AjaxResult.success(totalRecyclingCarbon.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BRecyclingCarbon> list =  excel.getRecyclingCarbonList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bRecyclingCarbonMapper.insertList(list);
        }
        return AjaxResult.success();
    }



    /**
     * 查询回收利用碳汇
     */
    @Override
    public BRecyclingCarbon selectBRecyclingCarbonById(Long id)
    {
        return bRecyclingCarbonMapper.selectBRecyclingCarbonById(id);
    }

    /**
     * 查询回收利用碳汇列表
     */
    @Override
    public List<BRecyclingCarbon> selectBRecyclingCarbonList(BRecyclingCarbon bRecyclingCarbon)
    {
        return bRecyclingCarbonMapper.selectBRecyclingCarbonList(bRecyclingCarbon);
    }

    /**
     * 新增回收利用碳汇
     */
    @Override
    public int insertBRecyclingCarbon(BRecyclingCarbon bRecyclingCarbon)
    {
        return bRecyclingCarbonMapper.insertBRecyclingCarbon(bRecyclingCarbon);
    }

    /**
     * 修改回收利用碳汇
     */
    @Override
    public int updateBRecyclingCarbon(BRecyclingCarbon bRecyclingCarbon)
    {
        return bRecyclingCarbonMapper.updateBRecyclingCarbon(bRecyclingCarbon);
    }

    /**
     * 批量删除回收利用碳汇
     */
    @Override
    public int deleteBRecyclingCarbonByIds(Long[] ids)
    {
        return bRecyclingCarbonMapper.deleteBRecyclingCarbonByIds(ids);
    }

    /**
     * 删除回收利用碳汇信息
     */
    @Override
    public int deleteBRecyclingCarbonById(Long id)
    {
        return bRecyclingCarbonMapper.deleteBRecyclingCarbonById(id);
    }



}
